package cn.campus.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


import cn.campus.pojo.User;
import cn.campus.service.UserService;

import com.alibaba.fastjson.JSON;

@Controller
@RequestMapping("/user")
public class UserController {

	@Resource
	private UserService userService;

	@RequestMapping("login")
	@ResponseBody
	public String login(User user,HttpSession httpSession){

		User user2 = userService.login(user);
		if(user2!=null){
			httpSession.setAttribute("user", user2);
			return JSON.toJSONString("登录成功");
		}else{
			return JSON.toJSONString("登录失败");
		}
	}

	@RequestMapping("getSessionUser")
	@ResponseBody
	public String getSessionUser(HttpSession httpSession){
		//从session中获取登录的账号
		User user = (User)httpSession.getAttribute("user");
		//如果user=null 说明用户还没登陆
		if(user==null){
			return JSON.toJSONString("用户未登录");
		}else{
			return JSON.toJSONString(user);
		}
	}

}
